<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\Tencent;
use app\model\AutoReply;
use app\model\Config;
use app\model\Help;
use app\model\User;

class ConfigController extends BaseController
{
	
	
	function getUserAgreement()
	{
		$row = Config::getFieldByName('user_agreement', 'value');
		$this->writeJson($row);
	}
	
	function getPrivacyPolicy()
	{
		$row = Config::getFieldByName('privacy_policy', 'value');
		$this->writeJson($row);
	}
	
	function getUserinfoExplain()
	{
		$row = Config::getFieldByName('userinfo_explain', 'value');
		$this->writeJson($row);
	}
	
	
	function getMpExplain()
	{
		$row = Config::getFieldByName('mp_explain', 'value');
		$this->writeJson($row);
	}
	
	function getRulesExplain()
	{
		$row = Config::getFieldByName('rules_explain', 'value');
		$this->writeJson($row);
	}
	
	#风险控制说明
	function getRiskContent()
	{
		$row = Config::getFieldByName('risk_content', 'value');
		$this->writeJson($row);
	}
	
	#风险控制案例
	function getRiskCase()
	{
		$row = Config::getFieldByName('risk_case', 'value');
		$row = explode(',', $row);
		$this->writeJson($row);
	}
	
	#积分说明
	function getScoreContent()
	{
		$row = Config::getFieldByName('score_content', 'value');
		$this->writeJson($row);
	}
	
	#邀请说明
	function getInviteContent()
	{
		$row = Config::getFieldByName('invite_content', 'value');
		$user = User::findOrFail($this->user_id);
		$invitecount = $user->child->count();
		$this->writeJson(['invite_content' => $row, 'count' => $invitecount, 'invitecode' => $user->invitecode]);
	}
	
	
	function getBannerList()
	{
		$rows = Config::getFieldByName('banners', 'value');
		$this->writeJson(explode(',', $rows));
	}
	
	function getCustomer()
	{
		$user = User::findOrFail($this->user_id);
		if (empty($user->customer_user_id)) {
			$customer_user_id = Config::getFieldByName('customer', 'value');
			$user->customer_user_id = $customer_user_id;
			$user->save();
		} else {
			$customer_user_id = $user->customer_user_id;
		}
		$this->writeJson(1, '', (string)$customer_user_id);
		
	}
	
	function autoReply()
	{
		$id = $this->request->get('id/d');
		$TencentIm = Tencent::getInstance();
		$row = AutoReply::findOrFail($id);
		$user = User::findOrFail($this->user_id);
		
		if (empty($id)){
			if (empty($user->customer_user_id)) {
				$customer_user_id = Config::getFieldByName('customer', 'value');
				$user->customer_user_id = $customer_user_id;
				$user->save();
			} else {
				$customer_user_id = $user->customer_user_id;
			}
			$auto_reply = Config::getFieldByName('auto_reply', 'value');
			$list = AutoReply::where(['pid'=>0])->select();
			$data = ['action'=>'auto_reply','data'=>['content'=>$auto_reply,'list'=>$list]];
			$ret = json_decode($TencentIm->send_msg($customer_user_id,$this->user_id,[
				[
					'MsgType'=>'TIMCustomElem',
					'MsgContent'=>[
						'Data'=>json_encode($data),
					],
				]
			]));
		}else{
			if ($row->child->isEmpty()){
				$ret = json_decode($TencentIm->send_msg($user->customer_user_id,$this->user_id,[
					[
						'MsgType'=>'TIMTextElem',
						'MsgContent'=>[
							'Text'=>$row->content,
						],
					]
				]));
			}else{
				$data = ['action'=>'auto_reply','data'=>['content'=>$row->title.':','list'=>$row->child]];
				$ret = json_decode($TencentIm->send_msg(11,10,[
					[
						'MsgType'=>'TIMCustomElem',
						'MsgContent'=>[
							'Data'=>json_encode($data),
						],
					]
				]));
			}
		}
		if ($ret->ErrorCode != 0) {
			$this->writeJson('接收IM消息失败');
		}
		$this->writeJson();
	}
	
	function getMobile()
	{
		$mobile = Config::getFieldByName('mobile', 'value');
		$consulttime = Config::getFieldByName('consulttime', 'value');
		$this->writeJson(['mobile' => explode(',', $mobile), 'consulttime' => $consulttime]);
	}
	
	function getHelp()
	{
		$rows = Help::order(['weigh' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
}
